package com.xk.view.impl;

import com.xk.dao.StudentDao;
import com.xk.dao.impl.StudentDaoImpl;
import com.xk.po.Course;
import com.xk.po.Student;
import com.xk.po.Teach;
import com.xk.po.Teacher;
import com.xk.util.DBUtil;
import com.xk.view.StudentView;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import java.util.Scanner;

public class StudentViewImpl implements StudentView {

    private Scanner input = new Scanner(System.in);

    @Override
    public void listCourseAll() {
        StudentDao dao = new StudentDaoImpl();
        List<Course> list = dao.listCourse(null);
        System.out.println("课程id\t课程名称");
        for (Course c : list){
            System.out.println(c.getCid()+"\t"+c.getCname());
        }
    }

    @Override
    public void listTeacherAll() {
        StudentDao dao = new StudentDaoImpl();
        List<Teacher> list = dao.listTeacher(null);
        System.out.println("老师id\t老师名称\t老师年龄\t教授课程id");
        for (Teacher c : list){
            System.out.println(c.getTid()+"\t"+c.getTname()+"\t"+c.getAge()+"\t"+c.getCid());
        }
    }

    @Override
    public void listTeachSome(Integer sid) {

        StudentDao dao = new StudentDaoImpl();
        List<Teach> list = dao.listTeach(sid);
        System.out.println("学生id\t课程id\t老师id\t记录号");
        for (Teach c : list){
            System.out.println(c.getSid()+"\t"+c.getCid()+"\t"+c.getTid()+"\t"+c.getRecord_id());
        }
    }

    @Override
    public Student login() {
        System.out.println("请输入你的学号：");
        int sid = input.nextInt();
        System.out.println("请输入密码");
        String password = input.next();

        StudentDao dao = new StudentDaoImpl();
        return dao.getStudentByIdByPass(sid, password);
    }

    @Override
    public void editTeach(Integer sid) {
        Scanner input = new Scanner(System.in);
        StudentDao dao = new StudentDaoImpl();
        Teach teach = dao.getTeachById(sid);
        //显示出当前选课信息
        List<Teach> list = dao.listTeach(sid);
        System.out.println("选课信息如下：");
        listTeachSome(sid);
        //判断，如果选课记录不满三条，就新建，如果满三条，就让学生选择是删除一条重新添加还是直接退出
        int len = list.size();
        if (len < 3){
            System.out.println("选课未完成，现在是否开始选课（y/n）：");
            if (input.next().equals("y")){
                System.out.println("请输入你要选的课程id：");
                int cid = input.nextInt();
                System.out.println("请输入你选择的老师id：");
                int tid = input.nextInt();
                if (dao.insertTeach(sid, cid, tid) != 0){
                    System.out.println("选课成功！");
                }
            }
        }
        else{
            System.out.println("你的课程已经选满了，是否需要修改（y/n）：");
            if (input.next().equals("y")) {
                System.out.println("请先删除一条记录（输入记录号）：");
                if (dao.removeTeach(input.nextInt()) != 0){
                    System.out.println("删除成功！");
                    System.out.println("请输入你要选的课程id：");
                    int cid = input.nextInt();
                    System.out.println("请输入你选择的老师id：");
                    int tid = input.nextInt();
                    if (dao.insertTeach(sid, cid, tid) != 0){
                        System.out.println("修改成功！");
                    }
                }
                else {
                    System.out.println("删除失败！");
                }
            }
        }
    }

    @Override
    public void updateStudentByPassword(Integer sid) {
        StudentDao dao = new StudentDaoImpl();
        Student student = dao.getStudentById(sid);
        System.out.println("请输入旧密码：");
        String oldPass = input.next();
        System.out.println("请输入新密码：");
        String password = input.next();
        System.out.println("请再次输入新密码：");
        String beginPassword = input.next();
        if (!student.getPassword().equals(oldPass)) {
            System.out.println("旧密码输入错误！");
        }else if(!password.equals(beginPassword)){
            System.out.println("两次输入密码不一致！");
        }else{
            int result = dao.updateStudentByPassword(sid, password);
            if (result > 0) {
                System.out.println("修改密码成功！");
            }else{
                System.out.println("修改密码失败！");
            }
        }
    }
}
